package com.example.onlinemonitoring.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Ejsensor;
import com.example.onlinemonitoring.dao.AppstationMapper;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.AppdeviceTemp;
import com.example.onlinemonitoring.model.Appstation;
import com.example.onlinemonitoring.model.Ejhistory;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.AppdeviceServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppdeviceTempServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppstationServiceImpl;
import com.example.onlinemonitoring.serviceImpl.EjsensorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "总站信息", tags = { "总站信息" })
@RestController
@RequestMapping("/station")
public class AppstationController {
    @Resource
    AppstationServiceImpl appstationServiceImpl;
    @Resource
    AppdeviceTempServiceImpl appdevicetempimpl;
    @Resource
    AppdeviceServiceImpl appdeviceimpl;
    @Resource
    AppstationMapper appstationmapper;
    @ApiOperation(value = "查询总站数据", notes = "查询总站数据")
    @ResponseBody
    @GetMapping("/selectData")
    public MyResult<Appstation> selectData() {
        return appstationServiceImpl.getList();
    }
    @ApiOperation(value = "出厂删除数据库表", notes = "出厂删除数据库表")
    @ResponseBody
    @RequestMapping(value = "/deleteTable", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    synchronized public MyResult<Integer> deleteTable(@RequestBody String jsonParam) {
        JSONArray json = JSONArray.parseArray(jsonParam);
        Example example = new Example(AppdeviceTemp.class);
        List<AppdeviceTemp> apptemp = appdevicetempimpl.getList(example).getData();
        for (int i = 0; i < json.size(); i++) {
            JSONObject object = (JSONObject) json.get(i);
            {
                Example appexample = new Example(Appdevice.class);
                Example.Criteria appcriteria = appexample.createCriteria();
                appcriteria.andEqualTo("installed", 1);
                int count = appdeviceimpl.selectCountByExample(appexample);
                if(count >= 1){
                    appstationmapper.setNeverAppearing();
                    MyResult<Integer> m = new MyResult<Integer>(); 
                    m.setMsg("操作失败！请先关闭已开启的设备！");
                    m.setCode(1);
                    return m;
                }
            }
            for(int j=0;j<apptemp.size();j++){
                if(object.getInteger("id").equals(apptemp.get(j).getId())&&object.getInteger("enable").equals(0)){

                    if(apptemp.get(j).getSensor() != null&&!apptemp.get(j).getSensor().equals("")){
                        appstationmapper.dropTable(apptemp.get(j).getSensor());
                    }
                    if(apptemp.get(j).getHistory() != null&&!apptemp.get(j).getHistory().equals("")){
                        appstationmapper.dropTable(apptemp.get(j).getHistory());
                    }
                    {
                        Example appexample = new Example(Appdevice.class);
                        Example.Criteria appcriteria = appexample.createCriteria();
                        appcriteria.andEqualTo("ename", apptemp.get(j).getEname());
                        appdeviceimpl.deleteByExample(appexample);
                    }
                    appdevicetempimpl.deleteByPrimaryKey(apptemp.get(j));
                }
            }
        }
        appstationmapper.setNeverAppearing();
        MyResult<Integer> m = new MyResult<Integer>(); 
        m.setMsg("操作成功");
        m.setCode(0);
        return m;
    }
}
